<template>
  <el-dialog
    title="渠道信息"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <div class="row">
        <el-form-item label="渠道编码" prop="entityIdentify" class="col-md-6">
          <el-input v-model="dataForm.channelCode" disabled placeholder="渠道编码"></el-input>
        </el-form-item>
        <el-form-item label="渠道名称" prop="entityName" class="col-md-6">
          <el-input v-model="dataForm.channelName"  placeholder="渠道名称"></el-input>
        </el-form-item>
      </div>

      <div class="row" v-if="showBtn">
        <el-button type="primary" @click="getKeys">获取密钥对</el-button>
      </div>

      <div class="row" v-if="showKey">
        <el-form-item  label="公钥" prop="remark" class="col-md-12">
            <el-input  type="textarea" readOnly :rows="5" v-model="dataForm.publicKey" placeholder="公钥"></el-input>
          </el-form-item>
          <el-form-item  label="私钥" prop="remark" class="col-md-12">
            <el-input  type="textarea" readOnly :rows="5" v-model="dataForm.privateKey" placeholder="私钥"></el-input>
          </el-form-item>
      </div>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button :plain="true" type="info" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { genKeyPair, getInfo, save } from '@/api/channel'

  export default {
    data () {
      return {
        showKey: false,
        showBtn: true,
        dataList: [],
        visible: false,
        dataForm: {
          id: null,
          channelCode: null,
          channelName: '',
          publicKey: '',
          privateKey: '',
          remark: '',
          isEffect: '1'
        }

      }
    },
    created () {

    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.visible = true
        this.dataList = []
        this.showKey = false
        this.showBtn = true

        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            let params = {
              id: this.dataForm.id + ''
            }
            getInfo(params).then(res => {
              if (res.data.data && res.data.resultCode === 0) {
                this.dataForm = res.data.data;
                this.showKey = true
              }
            })
          }
        })
      },

      // 获取实体类列表
      getKeys () {
        genKeyPair({}).then(res => {
          if (res.data.data && res.data.resultCode === 0) {
            this.dataForm.publicKey = res.data.data.publicKey
            this.dataForm.privateKey = res.data.data.privateKey
            this.showKey = true
            this.showBtn = false
          }
        })
      },

      deleteItem (index, rows) {
        rows.splice(index, 1)
      },

      // 表单提交
      dataFormSubmit () {
        save(JSON.stringify(this.dataForm)).then(res => {
          if (res.data && res.data.resultCode === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.dataForm = {
                  id: null,
                  channelCode: null,
                  channelName: '',
                  publicKey: '',
                  privateKey: '',
                  remark: '',
                  isEffect: '1'
                }
                this.showKey = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(res.data.resultMsg)
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .title{
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }
  .el-table__row .el-form-item__content{
    margin-left: 0 !important;
  }
  .el-table__row .el-form-item{
    margin-bottom: 0 !important;
  }
</style>
